﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithm_2.Easy_414_ThirdMax
{
    class MainProject
    {
        static void Main(string[] args)
        {
            int[] intArray1 = new int[] { 4, 9, 5 };
            int[] intArray2 = new int[] { 2,2,3,1};
            Console.WriteLine(ThirdMax(intArray2));
            Console.ReadKey();
        }

        public static int ThirdMax(int[] nums)
        {
            HashSet<int> vs = new HashSet<int>(nums);
            int[] set = vs.ToArray();
            Array.Sort(set);
            Array.Reverse(set);
            if (set.Length>=3)
            {
                return set[2];
            }
            return set[0];
        }
    }
}
